package cn.tedu.department.service.impl;

import cn.tedu.department.mapper.DepartmentMapper;
import cn.tedu.department.pojo.dto.DepartmentDTO;
import cn.tedu.department.pojo.vo.DepartmentVO;
import cn.tedu.department.service.DepartmentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Slf4j
@Service
public class DepartmentServiceImpl implements DepartmentService {
    @Autowired
    private DepartmentMapper departmentMapper;

    /**
     * 查询所有部门
     *
     * @param departmentDTO
     * @return
     */
    @Override
    public List<DepartmentVO> selectAll(DepartmentDTO departmentDTO) {
        List<DepartmentVO> departments = departmentMapper.selectAll(departmentDTO);
        return departments;
    }

    /**
     * 获取树形结构的部门列表
     *
     * @param departmentDTO
     * @return
     */
    public Map<String, Object> getTree(DepartmentDTO departmentDTO) {
        List<DepartmentVO> departments = departmentMapper.selectByPid(null); // 获取根节点
        if (departments.isEmpty()) {
            return Collections.emptyMap(); // 如果没有顶级节点，则返回空对象
        }

        DepartmentVO topDepartment = departments.get(0); // 取第一个顶级部门作为顶级节点
        Map<String, Object> root = new HashMap<>();
        root.put("id", topDepartment.getId());
        root.put("label", topDepartment.getName());
        root.put("status", topDepartment.getStatus());
        root.put("statusText", topDepartment.getStatus().equals("1") ? "启用" : "禁用");
        root.put("children", getChildren(topDepartment.getId())); // 递归获取子节点
        return root;
    }

    /**
     * 递归获取子级部门
     *
     * @param pid 父节点ID
     * @return 子节点列表
     */
    private List<Map<String, Object>> getChildren(Integer pid) {
        List<Map<String, Object>> children = new ArrayList<>();
        List<DepartmentVO> departments = departmentMapper.selectByPid(pid);
        for (DepartmentVO departmentVO : departments) {
            Map<String, Object> node = new HashMap<>();
            node.put("id", departmentVO.getId());
            node.put("pid", departmentVO.getPid());
            node.put("label", departmentVO.getName());
            node.put("status", departmentVO.getStatus());
            node.put("statusText", departmentVO.getStatus().equals("1") ? "启用" : "禁用");
            node.put("children", getChildren(departmentVO.getId())); // 递归获取子节点
            children.add(node);
        }
        return children;
    }

    /**
     * 更新
     *
     * @param departmentDTO
     */
    @Override
    public void update(DepartmentDTO departmentDTO) {
        if (departmentDTO.getId() == null) {
            insert(departmentDTO);
        } else {
            departmentMapper.update(departmentDTO);
        }
    }

    /**
     * 修改状态
     *
     * @param departmentDTO
     */
    @Override
    public void changeStatus(DepartmentDTO departmentDTO) {
        List<DepartmentVO> departmentVOs = departmentMapper.selectAll(departmentDTO);
        if (departmentVOs.isEmpty()) {
            return;
        }
        DepartmentVO departmentVO = departmentVOs.get(0);
        if (!"1".equals(departmentVO.getStatus())) {
            departmentDTO.setStatus("1");
        } else {
            departmentDTO.setStatus("-1");
        }

        departmentMapper.update(departmentDTO);
    }

    @Override
    public void insert(DepartmentDTO departmentDTO) {
        departmentDTO.setStatus("1");
        departmentDTO.setCreateTime(new Date());
        departmentMapper.insert(departmentDTO);
    }

    @Override
    public void delete(DepartmentDTO departmentDTO) {
        departmentDTO.setDeleteTime(new Date());
        departmentMapper.update(departmentDTO);
    }
}
